package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ITransferAccountsZfbBatchDAO;
import org.jsola.hr.dto.TransferAccountsZfbBatchAddDTO;
import org.jsola.hr.dto.TransferAccountsZfbBatchUpdateDTO;
import org.jsola.hr.entity.TransferAccountsZfbBatchDO;
import org.jsola.hr.query.TransferAccountsZfbBatchQuery;
import org.jsola.hr.service.ITransferAccountsZfbBatchService;
import org.jsola.hr.vo.TransferAccountsZfbBatchListVO;
import org.jsola.hr.vo.TransferAccountsZfbBatchVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 转账批次(支付宝)
 *
 * @author zhr
 */
@Service("hrTransferAccountsZfbBatchServiceImpl")
public class TransferAccountsZfbBatchServiceImpl implements ITransferAccountsZfbBatchService {

    @Autowired
    private ITransferAccountsZfbBatchDAO transferAccountsZfbBatchDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public TransferAccountsZfbBatchVO save(TransferAccountsZfbBatchAddDTO transferAccountsZfbBatchAddDTO, TokenUser tokenUser) {
        // 转DO
        TransferAccountsZfbBatchDO transferAccountsZfbBatchDO = transferAccountsZfbBatchAddDTO.to(TransferAccountsZfbBatchDO.class);
        // 保存
        transferAccountsZfbBatchDO = save(transferAccountsZfbBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return transferAccountsZfbBatchDO.to(TransferAccountsZfbBatchVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(TransferAccountsZfbBatchUpdateDTO transferAccountsZfbBatchUpdateDTO, TokenUser tokenUser) {
        //转DO
        TransferAccountsZfbBatchDO transferAccountsZfbBatchDO = transferAccountsZfbBatchUpdateDTO.to(TransferAccountsZfbBatchDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(transferAccountsZfbBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... transferAccountsZfbBatchIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) transferAccountsZfbBatchIds);
    }


    @Override
    public TransferAccountsZfbBatchVO selectById(String transferAccountsZfbBatchId, String siteId) {
        TransferAccountsZfbBatchDO transferAccountsZfbBatchDO = selectDOById(transferAccountsZfbBatchId, siteId);
        if (transferAccountsZfbBatchDO == null) {
            return null;
        }
        return transferAccountsZfbBatchDO.to(TransferAccountsZfbBatchVO.class);
    }

    @Override
    public List<TransferAccountsZfbBatchListVO> select(TransferAccountsZfbBatchQuery transferAccountsZfbBatchQuery, String siteId) {
        List<TransferAccountsZfbBatchDO> transferAccountsZfbBatchDOList = selectDO(transferAccountsZfbBatchQuery, siteId);
        if(CollectionUtils.isEmpty(transferAccountsZfbBatchDOList)) {
            return transferAccountsZfbBatchDOList == null ? null : new ArrayList<>();
        }
        return transferAccountsZfbBatchDOList.stream()
                .map(transferAccountsZfbBatchDO -> transferAccountsZfbBatchDO.to(TransferAccountsZfbBatchListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(TransferAccountsZfbBatchQuery transferAccountsZfbBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbBatchQuery,siteId);
        return transferAccountsZfbBatchDAO.selectCountByExample(example);
    }

    @Override
    public Page<TransferAccountsZfbBatchListVO> selectPage(TransferAccountsZfbBatchQuery transferAccountsZfbBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbBatchQuery,siteId);
        Page<TransferAccountsZfbBatchDO> page = transferAccountsZfbBatchDAO.selectPageByExample(example,
                transferAccountsZfbBatchQuery.getPageNo(),
                transferAccountsZfbBatchQuery.getPageSize());

        return page.to(TransferAccountsZfbBatchListVO.class);
    }

    @Override
    public TransferAccountsZfbBatchDO selectDOById(String transferAccountsZfbBatchId, String siteId) {
        return listById(transferAccountsZfbBatchId, siteId);
    }

    @Override
    public List<TransferAccountsZfbBatchDO> selectDO(TransferAccountsZfbBatchQuery transferAccountsZfbBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsZfbBatchQuery,siteId);
        return transferAccountsZfbBatchDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param transferAccountsZfbBatchQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(TransferAccountsZfbBatchQuery transferAccountsZfbBatchQuery, String siteId) {
        Example example = new Example(TransferAccountsZfbBatchDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,transferAccountsZfbBatchQuery.getOrders());
        return example;
    }
}




